package com.ruoyi.wvp.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.wvp.utils.PermissionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wvp.mapper.DutyCountMapper;
import com.ruoyi.wvp.domain.DutyCount;
import com.ruoyi.wvp.service.IDutyCountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.enums.DeletedEnum;
import com.ruoyi.common.utils.StringUtils;
import java.util.Objects;
/**
 * 督导员统计Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-23
 */
@Service
@Slf4j
public class DutyCountServiceImpl extends ServiceImpl<DutyCountMapper,DutyCount> implements IDutyCountService {
    @Autowired
    private DutyCountMapper dutyCountMapper;

    /**
     * 查询督导员统计
     * 
     * @param id 督导员统计主键
     * @return 督导员统计
     */
    @Override
    public DutyCount selectDutyCountById(Long id)    {
        return dutyCountMapper.selectDutyCountById(id);
    }

    /**
     * 查询督导员统计列表
     * 
     * @param dutyCount 督导员统计
     * @return 督导员统计
     */
    @Override
    public List<DutyCount> selectDutyCountList(DutyCount dutyCount)    {
        return dutyCountMapper.selectDutyCountList(dutyCount);
    }

    /**
     * 新增督导员统计
     * 
     * @param dutyCount 督导员统计
     * @return 结果
     */
    @Override
    public int insertDutyCount(DutyCount dutyCount)    {
        return dutyCountMapper.insertDutyCount(dutyCount);
    }

    /**
     * 修改督导员统计
     * 
     * @param dutyCount 督导员统计
     * @return 结果
     */
    @Override
    public int updateDutyCount(DutyCount dutyCount)    {
        return dutyCountMapper.updateDutyCount(dutyCount);
    }

    /**
     * 批量删除督导员统计
     * 
     * @param ids 需要删除的督导员统计主键
     * @return 结果
     */
    @Override
    public int deleteDutyCountByIds(Long[] ids)    {
        return dutyCountMapper.deleteDutyCountByIds(ids);
    }

    /**
     * 删除督导员统计信息
     * 
     * @param id 督导员统计主键
     * @return 结果
     */
    @Override
    public int deleteDutyCountById(Long id)    {
        return dutyCountMapper.deleteDutyCountById(id);
    }


       @Override
        public Page<DutyCount> pageList(Page<DutyCount> page, DutyCount dutyCount) {
            LambdaQueryWrapper<DutyCount> queryWrapper = getQueryWrapper(dutyCount);
            return page(page, queryWrapper);
        }

    @Override
    public DutyCount countStatistics(DutyCount dutyCount) {
        if(StringUtils.isNotBlank(dutyCount.getCountTimeStr())){
            LocalDate parse = LocalDate.parse(dutyCount.getCountTimeStr(), DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            dutyCount.setCountTime(parse);
        }
        if(Objects.isNull(dutyCount.getCountTime())){
            dutyCount.setCountTime(LocalDate.now().minusDays(1));
        }
        LambdaQueryWrapper<DutyCount> queryWrapper = getQueryWrapper(dutyCount);
        List<DutyCount> list = list(queryWrapper);
        DutyCount retDuct = new DutyCount();
        retDuct.setCount(0L);
        retDuct.setTotalCount(0L);
        for (DutyCount count : list) {
            retDuct.setCount(retDuct.getCount() + count.getCount());
            retDuct.setTotalCount(retDuct.getTotalCount()+ count.getTotalCount());
        }
        return retDuct;
    }

    @Override
    public void addCount(String streetCode,LocalDate countDate) {

        dutyCountMapper.addCount(streetCode,countDate);
    }

    @Override
        public List<DutyCount> listAll(DutyCount dutyCount) {
            return list(getQueryWrapper(dutyCount));
        }

        private  LambdaQueryWrapper<DutyCount> getQueryWrapper(DutyCount dutyCount) {
            LambdaQueryWrapper<DutyCount> queryWrapper = Wrappers.lambdaQuery(DutyCount.class);


//        日期
            if(Objects.nonNull(dutyCount.getCountTime())){
               queryWrapper.eq(DutyCount::getCountTime,dutyCount.getCountTime());
            }


//        在岗人数
        if(Objects.nonNull(dutyCount.getCount())){
           queryWrapper.eq(DutyCount::getCount,dutyCount.getCount());
        }
        if(StringUtils.isNotBlank(dutyCount.getArea())){
            queryWrapper.eq(DutyCount::getArea,dutyCount.getArea());
        }
//        if(Objects.nonNull(dutyCount.getCity())){
//            queryWrapper.eq(DutyCount::getCity,dutyCount.getCity());
//        }
        if(StringUtils.isNotBlank(dutyCount.getStreet())){
            queryWrapper.eq(DutyCount::getStreet,dutyCount.getStreet());
        }


            return queryWrapper;
        }
}
